package industry;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;

import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;

import skills.Skills;
import skills.SkillHandler;

public class IndustryBuff implements ActionListener {
	private static final PotionEffectType SPEED = PotionEffectType.FAST_DIGGING;
	private final Player player;
	private final Block block;
	private Industry industry;
	private final Timer timer;
	
	public enum Industry {
		ARCHAEOLOGY, MINING, WOODCUTTING;
	}

	public IndustryBuff(Player player, Block block, Industry industry) {
		this.player = player;
		this.block = block;
		this.industry = industry;
		timer = new Timer(0, this);
		timer.setRepeats(false);
		timer.start();
		applyBuff();
	}
	
	private void applyBuff() {
		int amplifier = 0;
		switch (industry) {
		case ARCHAEOLOGY:
			amplifier = SkillHandler.getSkill(player, Skills.ARCHAEOLOGY).level();
			break;
		case MINING:
			amplifier = SkillHandler.getSkill(player, Skills.MINING).level();
			break;
		case WOODCUTTING:
			amplifier = SkillHandler.getSkill(player, Skills.ARCHAEOLOGY).level();
			break;
		}
		amplifier = amplifier / 250 - 1;
		if (amplifier >= 0) {
			PotionEffect potion = new PotionEffect(SPEED, 15, amplifier);
			player.addPotionEffect(potion);
		}
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		applyBuff();
	}
	
	public Industry getIndustry() {
		return industry;
	}

	public void refresh(Block targetBlock) {
		if (targetBlock.equals(block)) {
			timer.restart();
		}
	}

	public void stop() {
		for (PotionEffect effect : player.getActivePotionEffects()) {
			if (effect.getType().equals(PotionEffectType.FAST_DIGGING)) {
				if (effect.getDuration() <= 15) {
					player.removePotionEffect(PotionEffectType.FAST_DIGGING);
				}
			}
		}
	}
}
